

#import "AdWhirlAdapterLifeStreet.h"
#import "AdWhirlAdNetworkConfig.h"
#import "AdWhirlView.h"
#import "LSMView.h"
#import "AdWhirlLog.h"
#import "AdWhirlAdNetworkAdapter+Helpers.h"
#import "AdWhirlAdNetworkRegistry.h"

@implementation AdWhirlAdapterLifeStreet

+ (AdWhirlAdNetworkType)networkType {
    return AdWhirlAdNetworkTypeLifeStreet;
}

+ (void)load {
    [[AdWhirlAdNetworkRegistry sharedRegistry] registerClass:self];
}

- (void)getAd {
    LSMView *lsmView = [LSMView requestAdWithDelegate:self];
    lsmView.frame = kAdWhirlViewDefaultFrame; // fit AD frame into default AdWhirl frame 
    self.adNetworkView = lsmView;
}

- (void)stopBeingDelegate {
    LSMView *lsmView = (LSMView *)self.adNetworkView;
    if (lsmView != nil) {
        lsmView.delegate = nil;
    }
}

- (void)dealloc {
    [super dealloc];
}

#pragma mark LSMViewDelegate required methods
- (NSString *)slotUrl {
    NSString *slotURL = nil;
    if ([adWhirlDelegate respondsToSelector:@selector(lifeStreetSlotID)]) {
        slotURL = [adWhirlDelegate lifeStreetSlotID];
    } else if (networkConfig.credentials != nil) {
        slotURL = [networkConfig.credentials objectForKey:@"SlotURL"];  
    } 
    return slotURL;
}

- (NSString*) lsmAppId {
    NSString *lsmId = nil;
    if ([adWhirlDelegate respondsToSelector:@selector(lifeStreetLSMID)]) {
        lsmId = [adWhirlDelegate lifeStreetLSMID];
    } else if (networkConfig.credentials != nil) {
        lsmId = [networkConfig.credentials objectForKey:@"LSMID"];    
    }    
    return lsmId;
}

- (NSString*) iTunesId {
    NSString *iTunesId = nil;
    if ([adWhirlDelegate respondsToSelector:@selector(lifeStreetITunesId)]) {
        iTunesId = [adWhirlDelegate lifeStreetITunesId];
    } else if (networkConfig.credentials != nil) {
        iTunesId = [networkConfig.credentials objectForKey:@"iTunesID"];
    }     
    return iTunesId;
}

- (UIViewController *)currentViewControllerForAd {
	return [adWhirlDelegate viewControllerForPresentingModalView];
}

#pragma mark LSMViewDelegate notification methods

- (void)didReceiveAd:(LSMView *)adView {
    [adWhirlView adapter:self didReceiveAdView:adView];
}

- (void)didFailToReceiveAd:(LSMView *)adView {
    [adWhirlView adapter:self didFailAd:nil];
}

- (void)willPresentFullScreenModalFromAd:(LSMView *)adView {
    [self helperNotifyDelegateOfFullScreenModal];
}

- (void)didDismissFullScreenModalFromAd:(LSMView *)adView {
    [self helperNotifyDelegateOfFullScreenModalDismissal];
}

#pragma mark LSMViewDelegate optional config methods


- (BOOL)respondsToSelector:(SEL)selector {
    if ((selector == @selector(locationLatitude)
         || selector == @selector(locationLongitude)
         || selector == @selector(locationTimestamp))
        && (!adWhirlConfig.locationOn
            || ![adWhirlDelegate respondsToSelector:@selector(locationInfo)])) {
            return NO;
        }
    else if (selector == @selector(postalCode)
             && ![adWhirlDelegate respondsToSelector:@selector(postalCode)]) {
        return NO;
    }
    else if (selector == @selector(areaCode)
             && ![adWhirlDelegate respondsToSelector:@selector(areaCode)]) {
        return NO;
    }
    else if (selector == @selector(dateOfBirth)
             && ![adWhirlDelegate respondsToSelector:@selector(dateOfBirth)]) {
        return NO;
    }
    else if (selector == @selector(gender)
             && ![adWhirlDelegate respondsToSelector:@selector(gender)]) {
        return NO;
    }
    else if (selector == @selector(keywords)
             && ![adWhirlDelegate respondsToSelector:@selector(keywords)]) {
        return NO;
    }
    return [super respondsToSelector:selector];
}

- (double)locationLatitude {
    return [adWhirlDelegate locationInfo].coordinate.latitude;
}

- (double)locationLongitude {
    return [adWhirlDelegate locationInfo].coordinate.longitude;
}

- (NSDate *)locationTimestamp {
    return [adWhirlDelegate locationInfo].timestamp;
}

- (NSString *)postalCode {
    return [adWhirlDelegate postalCode];
}

- (NSString *)areaCode {
    return [adWhirlDelegate areaCode];
}

- (NSDate *)dateOfBirth {
    return [adWhirlDelegate dateOfBirth];
}

- (NSString *)gender {
    return [adWhirlDelegate gender];
}

- (NSString *)keywords {
    return [adWhirlDelegate keywords];
}

@end
